import request from '@/utils/request'

// 获取传感器最新数据
export function getLatestSensorData(serialNumber) {
  return request({
    url: `/sensor-data/latest/${serialNumber}`,
    method: 'get'
  })
}

// 获取所有土壤传感器数据
export async function getSoilSensorsData() {
  const sensors = {
    nitrogen: '327061330932536077',
    phosphorus: '327061330932536078',
    potassium: '327061330932536079'
  };

  try {
    const [nitrogen, phosphorus, potassium] = await Promise.all([
      getLatestSensorData(sensors.nitrogen),
      getLatestSensorData(sensors.phosphorus),
      getLatestSensorData(sensors.potassium)
    ]);

    return {
      nitrogen: nitrogen.data?.value || 0,
      phosphorus: phosphorus.data?.value || 0,
      potassium: potassium.data?.value || 0
    };
  } catch (error) {
    console.error('获取传感器数据失败:', error);
    return {
      nitrogen: 0,
      phosphorus: 0,
      potassium: 0
    };
  }
}

// 获取空气温湿度数据
export async function getAirData() {
  const sensors = {
    temperature: '327061330933584659',
    humidity: '327061330933584658'
  };

  try {
    const [temperature, humidity] = await Promise.all([
      getLatestSensorData(sensors.temperature),
      getLatestSensorData(sensors.humidity)
    ]);

    return {
      temperature: temperature.data?.value || 0,
      humidity: humidity.data?.value || 0
    };
  } catch (error) {
    console.error('获取空气数据失败:', error);
    return {
      temperature: 0,
      humidity: 0
    };
  }
}

// 获取所有实时传感器数据
export async function getAllSensorData() {
  const sensors = {
    nitrogen: '327061330932536077',
    phosphorus: '327061330932536078',
    potassium: '327061330932536079',
    temperature: '327061330933584659',
    humidity: '327061330933584658'
  };

  try {
    const [nitrogen, phosphorus, potassium, temperature, humidity] = await Promise.all([
      getLatestSensorData(sensors.nitrogen),
      getLatestSensorData(sensors.phosphorus),
      getLatestSensorData(sensors.potassium),
      getLatestSensorData(sensors.temperature),
      getLatestSensorData(sensors.humidity)
    ]);

    return [
      ['土壤含氮量', (nitrogen.data?.value || 0).toFixed(1), 'mg/kg'],
      ['土壤含磷量', (phosphorus.data?.value || 0).toFixed(1), 'mg/kg'],
      ['土壤含钾量', (potassium.data?.value || 0).toFixed(1), 'mg/kg'],
      ['空气温度', (temperature.data?.value / 10 || 0).toFixed(1), '°C'],
      ['空气湿度', (humidity.data?.value / 10 || 0).toFixed(1), '%RH'],
      ['土壤含水率', '71.4', '%RH'],
      ['土壤电导率', '1.6', 'us/cm'],
      ['土壤PH值', '6.5', 'PH'],
      ['光照度', '9881', 'Lux'],
      ['二氧化碳', '453.1', 'ppm']
    ];
  } catch (error) {
    console.error('获取传感器数据失败:', error);
    // 发生错误时返回所有数据的默认值
    return [
      ['土壤含氮量', '0.0', 'mg/kg'],
      ['土壤含磷量', '0.0', 'mg/kg'],
      ['土壤含钾量', '0.0', 'mg/kg'],
      ['土壤含水率', '71.4', '%RH'],
      ['土壤电导率', '1.6', 'us/cm'],
      ['土壤PH值', '6.5', 'PH'],
      ['光照度', '9881', 'Lux'],
      ['二氧化碳', '453.1', 'ppm'],
      ['空气温度', '0.0', '°C'],
      ['空气相对湿度', '0.0', '%RH']
    ];
  }
} 